package com.fenghongzhang.a1807;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btn1 = (Button) findViewById(R.id.btn1);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                AlertDialog.Builder builder1 = builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("标题")
                        .setMessage("内内");
                builder1.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
                    }
                });

                builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "消极", Toast.LENGTH_SHORT).show();
                    }
                });

                AlertDialog alertDialog = builder1.create();

                //show
                alertDialog.show();
            }
        });
    }

    public void btn2(View view) {

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        AlertDialog.Builder builder1 = builder.setIcon(R.mipmap.ic_launcher)
                .setTitle("标题");
        builder1.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });

        builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "消极", Toast.LENGTH_SHORT).show();
            }
        });

        final String[] s = new String[]{"可乐","雪碧","肥宅水"};
        builder1.setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
            }
        });



        AlertDialog alertDialog = builder1.create();

        //show
        alertDialog.show();

    }

    //多选
    public void btn3(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        AlertDialog.Builder builder1 = builder.setIcon(R.mipmap.ic_launcher)
                .setTitle("标题");
        builder1.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });

        builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "消极", Toast.LENGTH_SHORT).show();
            }
        });


        String[] s = {"篮球","足球","排球","唢呐"};
        final boolean[] booleans = new boolean[]{true,true,true,false}; //小写
        builder1.setMultiChoiceItems(s, booleans, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                booleans[which] = isChecked;
            }
        });

        AlertDialog alertDialog = builder1.create();

        //show
        alertDialog.show();
    }

    //自定义内容
    public void btn4(View view) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        final AlertDialog.Builder builder1 = builder.setIcon(R.mipmap.ic_launcher)
                .setTitle("标题");
        builder1.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });

        builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "消极", Toast.LENGTH_SHORT).show();
            }
        });

        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null);
        builder1.setView(inflate);

        final AlertDialog alertDialog = builder1.create();
        //点击事件
        ImageView imageView = inflate.findViewById(R.id.img);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消对话框
                alertDialog.dismiss();
            }
        });
        //show
        alertDialog.show();
    }

    //进度对话框
    public void btn5(View view) {
        final ProgressDialog progressDialog = new ProgressDialog(this);

        progressDialog.setMax(100);
        progressDialog.setProgress(10);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        //定时器
        final Timer timer = new Timer(); //闹钟
        /**
         * 1,任务
         * 2,延迟执行
         * 3,每隔多少毫秒执行一次
         */
        timer.schedule(new TimerTask() {
            int index = 0;
            @Override
            public void run() {
                index+= 10;
                progressDialog.setProgress(index);
                if(index >= progressDialog.getMax()){
                    timer.cancel();
                    progressDialog.dismiss();
                }
            }
        },0,2000);


        progressDialog.show();
    }

    //日期对话框
    public void btn6(View view) {
        //日期对象
        Calendar instance = Calendar.getInstance();

//        instance.get(Calendar.YEAR);
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this, year+":"+(month+1)+":"+dayOfMonth, Toast.LENGTH_SHORT).show();

            }
        }, instance.get(Calendar.YEAR), instance.get(Calendar.MONTH), instance.get(Calendar.DAY_OF_MONTH)); //月份是从0开始

        datePickerDialog.show();


    }

    public void btn7(View view) {

        Calendar instance = Calendar.getInstance();

        TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
            }
        }, instance.get(Calendar.HOUR), instance.get(Calendar.MINUTE), false);

        timePickerDialog.show();
    }

    public void btn8(View view) {

        MyDialog myDialog = new MyDialog(this);
//        myDialog.setMessage("呵呵呵呵");
        myDialog.setStrMessage("呵呵呵呵呵");
        myDialog.setStrTitle("哈哈哈");
        
        myDialog.setYesClickListener(new MyDialog.YesClickListener() {
            @Override
            public void yesClick() {
                Toast.makeText(MainActivity.this, "哈哈哈", Toast.LENGTH_SHORT).show();
            }
        });
        
        myDialog.setNoClickListener(new MyDialog.NoClickListener() {
            @Override
            public void noClick() {
                Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_SHORT).show();
            }
        });
        myDialog.show();
    }
}
